package com.aerospace.serviceprovider.io_nio;

import java.io.*;
import java.nio.ByteBuffer;

public class FileChannel {

    public static void method1(){
        InputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(""));
            byte[] buf = new byte[1024];
            int bytesRead = in.read(buf);
            while (bytesRead != -1){
                for (int i = 0; i<bytesRead ; i++)
                    System.out.println((char)buf[i]);
                bytesRead = in.read(buf);
            }
        }catch (IOException e){

        }finally {
            try {
                if(in != null){
                    in.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

    public static void method2(){
        RandomAccessFile aFile = null;
        try{
            aFile = new RandomAccessFile("","rw");
            java.nio.channels.FileChannel fileChannel = aFile.getChannel();
            ByteBuffer buf = ByteBuffer.allocate(1024);
            int bytesRead = fileChannel.read(buf);
            System.out.println(bytesRead);
            while(bytesRead != -1){
                buf.flip();
                while (buf.hasRemaining()){
                    System.out.println();
                }
                buf.compact();
                bytesRead = fileChannel.read(buf);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                if (aFile != null){
                    aFile.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
}
